package com.cuit.qx.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuit.qx.bean.WeatherStation;
import com.cuit.qx.entity.*;
import com.cuit.qx.service.*;
import com.cuit.qx.utils.ExcelReadUtil;
import com.cuit.qx.utils.ExcelUtil;
import com.cuit.qx.utils.HttpUtil;
import com.cuit.qx.utils.JsonUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ：long
 * @date ：Created in 2021/11/24 0024 10:15
 * @description：
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/demo")
@RestController
public class DemoController {

    @Autowired
    WeatherStationService stationService;

    @Autowired
    CorrectForecastService correctForecastService;
    @Autowired
    RiskWarningService riskWarningService;
    @Autowired
    CheckoutService checkoutService;

    @RequestMapping("/testCheckOut")
    public String test(){
        List<WeatherStation> list = stationService.list();
        String[] time={"02","08","14","20"};
        String[] orders={"0.1","1.0","5.0","10.0","25","50"};
        int[] order={0,1,5,10,25,50};
        for (int j = 0; j <= 5; j++) {
            List<Checkout> li=new ArrayList<>();
            for (WeatherStation weatherStation : list) {
                for (int i = 0; i <= 3; i++) {
                    for (String i1 : time) {
                        for (int q = 0; q < orders.length ; q++) {
                            for (int k = 0; k <= 1; k++) {
                                Checkout checkout=new Checkout();
                                checkout.setPattern(String.valueOf(i));
                                checkout.setCheckoutDate("2021-12-06");
                                checkout.setCheckoutTime(i1);
                                checkout.setCheckoutStaion(weatherStation.getStationName());
                                checkout.setTimeliness("12");
                                checkout.setOrders(orders[q]);
                                checkout.setCheckoutType(String.valueOf(k));
                                checkout.setCheckoutMethod(String.valueOf(j));
                                if (q==orders.length-1){
                                    checkout.setTime12(new DecimalFormat("0.0").format(getfloat(50,25,"25","a")));
                                    checkout.setTime24(new DecimalFormat("0.0").format(getfloat(50,25,"25","a")));
                                    checkout.setTime36(new DecimalFormat("0.0").format(getfloat(50,25,"25","a")));
                                    checkout.setTime48(new DecimalFormat("0.0").format(getfloat(50,25,"25","a")));
                                    checkout.setTime60(new DecimalFormat("0.0").format(getfloat(50,25,"25","a")));
                                    checkout.setTime72(new DecimalFormat("0.0").format(getfloat(50,25,"25","a")));
                                }else{
                                    checkout.setTime12(new DecimalFormat("0.0").format(getfloat(order[q+1],order[q],orders[q],"b")));
                                    checkout.setTime24(new DecimalFormat("0.0").format(getfloat(order[q+1],order[q],orders[q],"b")));
                                    checkout.setTime36(new DecimalFormat("0.0").format(getfloat(order[q+1],order[q],orders[q],"b")));
                                    checkout.setTime48(new DecimalFormat("0.0").format(getfloat(order[q+1],order[q],orders[q],"b")));
                                    checkout.setTime60(new DecimalFormat("0.0").format(getfloat(order[q+1],order[q],orders[q],"b")));
                                    checkout.setTime72(new DecimalFormat("0.0").format(getfloat(order[q+1],order[q],orders[q],"b")));
                                }

                                li.add(checkout);
                            }
                        }

                    }
                }
            }
            checkoutService.saveBatch(li,1500);
        }

        return "chenggong";
    }


    @RequestMapping("/readJsonToData")
    public void readJsonToData(){
        String data = JsonUtil.readJsonFile(new File("C:\\Users\\Administrator\\Desktop\\wuzhou\\forecast_out.json"));
        patternTransitionJob(data);
    }

    @Autowired
    CorrectForecastService service;
    @Autowired
    InterfaceMonitoringService interfaceMonitoringService;

    public void patternTransitionJob(String data){
        InterfaceMonitoring monitoring = new InterfaceMonitoring();
        monitoring.setExecutionTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        monitoring.setId("482a6c6b1640682720019");
        monitoring.setInterfaceNum(2);
        try {
//            data = new HttpUtil().doGet("http://127.0.0.1:8001/correctForecast/rain_forecast?micapszip=" + micapszip + "&startnum=" + startDate);
            String[] patterns = {"ec", "grapes", "huanan", "correct"};

            if (null == data) {
//                LOG.info("订正预报接口请求数据为空" + data);
                monitoring.setDescription("订正预报接口数据为空");
                return;
            }
            monitoring.setState(0);
            monitoring.setDescription("接口采集正常");

            for (int i = 0; i < patterns.length; i++) {
                JSONObject jsonObject = JSONObject.fromObject(data);
                Object ec = jsonObject.get(patterns[i]);
                JSONObject jsonObject1 = JSONObject.fromObject(ec);
                String station = jsonObject1.get("station").toString();

                HashMap map = JSON.parseObject(station, HashMap.class);

                Iterator iterator = map.keySet().iterator();

                while (iterator.hasNext()) {
                    Object key = iterator.next();
                    CorrectForecast correctForecast = new CorrectForecast();
                    correctForecast.setPattern(patternTransition(patterns[i]));//模式转换
                    correctForecast.setStationId((String) key);
                    String[] bytes = map.get(key).toString().replace("[", "").replace("]", "").split(",");
                    correctForecast.setTime3(bytes[0]);
                    correctForecast.setTime6(bytes[1]);
                    correctForecast.setTime9(bytes[2]);
                    correctForecast.setTime12(bytes[3]);
                    correctForecast.setTime15(bytes[4]);
                    correctForecast.setTime18(bytes[5]);
                    correctForecast.setTime21(bytes[6]);
                    correctForecast.setTime24(bytes[7]);
                    correctForecast.setTime27(bytes[8]);
                    correctForecast.setTime30(bytes[9]);
                    correctForecast.setTime33(bytes[10]);
                    correctForecast.setTime36(bytes[11]);
                    correctForecast.setTime39(bytes[12]);
                    correctForecast.setTime42(bytes[13]);
                    correctForecast.setTime45(bytes[14]);
                    correctForecast.setTime48(bytes[15]);
                    correctForecast.setTime51(bytes[16]);
                    correctForecast.setTime54(bytes[17]);
                    correctForecast.setTime57(bytes[18]);
                    correctForecast.setTime60(bytes[19]);
                    correctForecast.setTime63(bytes[20]);
                    correctForecast.setTime66(bytes[21]);
                    correctForecast.setTime69(bytes[22]);
                    correctForecast.setTime72(bytes[23]);
                    correctForecast.setId(UUID.randomUUID().toString().substring(0, 12));
                    LambdaQueryWrapper<WeatherStation> queryWrapper = new LambdaQueryWrapper();
                    LambdaQueryWrapper<WeatherStation> eq = queryWrapper.eq(WeatherStation::getStationNumber, (String) key);
                    WeatherStation one = stationService.getOne(eq);
                    correctForecast.setLat(String.valueOf(one.getLatitude()));
                    correctForecast.setLon(String.valueOf(one.getLongitude()));
                    correctForecast.setStartTime(Integer.parseInt("08"));
                    correctForecast.setStartDate("2022-02-22");
                    correctForecast.setRemark(one.getStationCounty());
                    correctForecast.setStaionName(one.getStationName());
                    service.save(correctForecast);
                }
            }
        } catch (Exception e) {
            monitoring.setState(1);
            monitoring.setDescription("接口异常" + e);
//            LOG.error("订正预报采集异常", e);
        }


        interfaceMonitoringService.updateById(monitoring);

    }

    public int patternTransition(String pattern) {
        int pa = 0;
        switch (pattern) {
            case "grapes":
                pa = 0;
                break;
            case "ec":
                pa = 1;
                break;
            case "huanan":
                pa = 2;
                break;
            case "correct":
                pa = 3;
                break;
        }
        return pa;
    }

    @Autowired
    InspectionService inspectionService;

    @RequestMapping("/list")
    public String createData(){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.ne("station_type","0");
        List<WeatherStation> list = stationService.list(queryWrapper);
        SimpleDateFormat sdf=new SimpleDateFormat();
        int[] time={2,8,14,20};
        for (int j = 1; j <= 28; j++) {
            List<CorrectForecast> li=new ArrayList<>();
            for (WeatherStation weatherStation : list) {
                for (int i = 0; i <= 3; i++) {
                    for (int i1 : time) {
                        CorrectForecast correctForecast=new CorrectForecast();
                        correctForecast.setPattern(i);
                        if (j<=9){
                            correctForecast.setStartDate("2022-02-0"+j);
                        }else{
                            correctForecast.setStartDate("2022-02-"+j);
                        }
                        correctForecast.setStartTime(i1);
                        correctForecast.setStationId(weatherStation.getStationNumber());
                        correctForecast.setStaionName(weatherStation.getStationName());
                        correctForecast.setLat(String.valueOf(weatherStation.getLatitude()));
                        correctForecast.setLon(String.valueOf(weatherStation.getLongitude()));
                        correctForecast.setTime12(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime24(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime36(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime48(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime60(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime72(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime3(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime6(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime9(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime12(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime15(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime18(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime21(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime27(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime30(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime33(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime39(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime42(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime45(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime51(new DecimalFormat("0.0").format(Math.random()*10));
                        correctForecast.setTime54(new DecimalFormat("0.0").format(Math.random()*10));
                        li.add(correctForecast);
                    }
                }
            }
            correctForecastService.saveBatch(li);
        }
        return "成功";
    }

    @RequestMapping("/warnList")
    public String createWarnData(){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.ne("station_type","0");
        List<WeatherStation> list = stationService.list(queryWrapper);
        SimpleDateFormat sdf=new SimpleDateFormat();
        int[] time={12,24,36,48,60,72};
        String[] start={"02","08","14","20"};
        for (int j = 1; j < 30; j++) {
            List<RiskWarning> li=new ArrayList<>();
            List<WeatherStation> list1 = new ArrayList<>();
            list1.addAll(list);
            for (int k = 0; k < 20; k++) {
                int index=(int)(Math.random()*(list1.size()-5))+1;
                System.out.println("下标："+index+"  集合大小："+list1.size());
                WeatherStation weatherStation=list1.get(index);
                list1.remove(index);
                for (int i = 0; i <= 3; i++) {
                    for (String i1 : start) {
                        for (int i2 : time) {
                            RiskWarning riskWarning=new RiskWarning();
                            riskWarning.setPattern(String.valueOf(i));
                            if (j<=9){
                                riskWarning.setStartDate("2021-12-0"+j);
                            }else{
                                riskWarning.setStartDate("2021-12-"+j);
                            }
                            riskWarning.setStartTime(i1);
                            riskWarning.setStationId(weatherStation.getStationNumber());
                            riskWarning.setStaionName(weatherStation.getStationName());
                            riskWarning.setLat(String.valueOf(weatherStation.getLatitude()));
                            riskWarning.setLon(String.valueOf(weatherStation.getLongitude()));
                            riskWarning.setTime(String.valueOf(i2));
                            riskWarning.setReiskId(1);
                            riskWarning.setTimeValue(new DecimalFormat("0.0").format(Math.random()*80));
                            li.add(riskWarning);
                        }
                    }
                }
            }
            riskWarningService.saveBatch(li);
        }

        return "成功";
    }

    @RequestMapping("/save")
    public String ll(){
        File file=new File("C:\\Users\\Administrator\\Desktop\\自动站.xlsx");
        List<WeatherStation> weatherStations = ExcelReadUtil.readExcel(file, 1, null);
        stationService.saveBatch(weatherStations);
        return "chenggong";
    }

    public static float getfloat(int max,int min,String mi,String type){
        float f=Float.parseFloat(mi);
        Random random=new Random();
        int i;
        if (type.equals("a")){
            i = random.nextInt(max - min)+50;
            float result=random.nextFloat()+i;
            return result;
        }else {
            i = random.nextInt(max - min);
            float result=random.nextFloat()+i;
            return result>=max?result-f:result<=f?result+f:result;
        }

    }
    public static void main(String[] args) throws IOException {
        for (int i = 0; i <10 ; i++) {
            System.out.println(getfloat(50,25,"25","a"));
        }
    }

    public static List<String> readFileByLines(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            InputStreamReader fReader = new InputStreamReader(new FileInputStream(file), "GB2312");
            reader = new BufferedReader(fReader);
            String tempString = null;
            List<String> list = new ArrayList();
            while ((tempString = reader.readLine()) != null) {
                list.add(tempString);
            }
            reader.close();
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return null;
    }



}

